package com.atguigu.aclservice.controller;


import com.atguigu.aclservice.entity.Menu;
import com.atguigu.aclservice.entity.vo.AssginMenuVo;
import com.atguigu.aclservice.service.MenuService;
import com.atguigu.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author fxy
 * @since 2022-10-27
 */
@Api(tags = "菜单管理接口")
@Slf4j
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping("/acl/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

//    @Log(title = "给角色分配菜单权限",businessType = BusinessType.ASSGIN)
    @ApiOperation("给角色分配菜单权限")
    @PostMapping("/doAssign")
    public R doAssign(@RequestBody AssginMenuVo assginMenuVo) {
        menuService.doAssign(assginMenuVo);
        return R.ok();
    }

//    @Log(title = "根据角色获取菜单",businessType = BusinessType.FIND)
    @ApiOperation("根据角色获取菜单")
    @GetMapping("/toAssign/{roleId}")
    public R toAssign(@PathVariable Long roleId) {
        List<Menu> list = menuService.findMenuByRoleId(roleId);
        return R.ok().data("list",list);
    }

    @ApiOperation("菜单列表")
    @GetMapping("findNodes")
    public R findNodes(){
        List<Menu> list= menuService.findNodes();
        return R.ok().data("list",list);
    }

//    @Log(title = "添加菜单",businessType = BusinessType.INSERT)
    @ApiOperation("添加菜单")
    @PostMapping("save")
    public R save(@RequestBody Menu menu){
        menuService.save(menu);
        return R.ok();
    }
//    @Log(title = "根据id查询菜单",businessType = BusinessType.FIND)
    @ApiOperation("根据id查询菜单")
    @GetMapping("findNode/{id}")
    public R findNode(@PathVariable Long id){
        Menu menu = menuService.getById(id);
        return R.ok().data("menu",menu);
    }

//    @Log(title = "修改菜单",businessType = BusinessType.UPDATE)
    @ApiOperation("修改菜单")
    @PostMapping("update")
    public R update(@RequestBody Menu menu){
        menuService.updateById(menu);
        return R.ok();
    }

//    @Log(title = "删除菜单",businessType = BusinessType.DELETE)
    @ApiOperation("删除菜单")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable Long id){
        menuService.removeMenuById(id);
        return R.ok();
    }
}

